
import { _decorator, DynamicAtlasManager, macro } from "cc";
import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { JsonUtil } from "../../../../../extensions/oops-plugin-framework/assets/core/utils/JsonUtil";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { CCVMParentComp } from "../../../../../extensions/oops-plugin-framework/assets/module/common/CCVMParentComp";
import { TableRoleModel } from "../../common/table/TableRoleModel";
import { GameEvent } from "../../../type/GameEvent";
import { smc } from "../../common/ecs/SingletonModuleComp";
import { TableLordConf } from "../../common/table/TableLordConf";
import { TableBookConf } from "../../common/table/TableBookConf";
import { TableHeroStarExt } from "../../common/table/TableHeroStarExt";
import { TableBattleConf } from "../../common/table/TableBattleConf";
import { TableSkill } from "../../common/table/TableSkill";
import { TableBuff } from "../../common/table/TableBuff";
import { TableItemConf } from "../../common/table/TableItemConf";
import { TableEquipmentConf } from "../../common/table/TableEquipmentConf";
import { TableEquipmentColorConf } from "../../common/table/TableEquipmentColorConf";
import { TableEquipmentLvConf } from "../../common/table/TableEquipmentLvConf";


const { ccclass, property } = _decorator;

macro.CLEANUP_IMAGE_CACHE = false;
DynamicAtlasManager.instance.enabled = true;

/** 
 * 游戏资源加载 
 * 1、加载游戏首场景资源
 * 2、获取服务器游戏数据
 * 3、加载游戏中界面
 * 4、加载战斗场景
 */
@ccclass('LoadingViewComp')
@ecs.register('LoadingView', false)
export class LoadingViewComp extends CCVMParentComp {
    /** VM 组件绑定数据 */
    data: any = {
        /** 加载资源当前进度 */
        finished: 0,
        /** 加载资源最大进度 */
        total: 0,
        /** 加载资源进度比例值 */
        progress: "0",
        /** 加载流程中提示文本 */
        prompt: ""
    };

    private progress: number = 0;

    start() {
        this.initEvent(GameEvent.LoadOver, this.enter);
    }


    enter() {
        this.addEvent();
        this.loadRes();
    }

    private addEvent() {
        this.initEvent(GameEvent.LoginSuccess, this.onHandler);
    }

    private onHandler(event: string, args: any) {
        switch (event) {
            case GameEvent.LoginSuccess:

                // 加载主界面
                smc.account.loadMain();

                // 加载战斗场景
                smc.scene.load(1);

                this.node.destroy();
                break;
        }
    }

    /** 加载资源 */
    private async loadRes() {
        this.data.progress = 0;
        await this.loadCustom();
        this.loadGameRes();
    }

    /** 加载游戏本地JSON数据（自定义内容） */
    private loadCustom() {
        // 加载游戏本地JSON数据的多语言提示文本
        this.data.prompt = oops.language.getLangByID("loading_load_json");

        return new Promise(async (resolve, reject) => {
            await JsonUtil.loadAsync(TableRoleModel.TableName);
            await JsonUtil.loadAsync(TableLordConf.TableName);
            await JsonUtil.loadAsync(TableBookConf.TableName);
            await JsonUtil.loadAsync(TableHeroStarExt.TableName);
            await JsonUtil.loadAsync(TableBattleConf.TableName);
            await JsonUtil.loadAsync(TableSkill.TableName);
            await JsonUtil.loadAsync(TableBuff.TableName);
            await JsonUtil.loadAsync(TableItemConf.TableName);
            await JsonUtil.loadAsync(TableEquipmentConf.TableName);
            await JsonUtil.loadAsync(TableEquipmentLvConf.TableName);
            await JsonUtil.loadAsync(TableEquipmentColorConf.TableName);
            resolve(null);







        });
    }

    /** 加载初始游戏内容资源 */
    private loadGameRes() {
        // 加载初始游戏内容资源的多语言提示文本
        this.data.prompt = oops.language.getLangByID("loading_load_game");

        oops.res.loadDir("game", this.onProgressCallback.bind(this), this.onCompleteCallback.bind(this));
    }

    /** 加载进度事件 */
    private onProgressCallback(finished: number, total: number, item: any) {
        this.data.finished = finished;
        this.data.total = total;

        var progress = finished / total;
        if (progress > this.progress) {
            this.progress = progress;
            this.data.progress = (progress * 100).toFixed(2);
        }
    }

    /** 加载完成事件 */
    private onCompleteCallback() {
        // 获取用户信息的多语言提示文本
        this.data.prompt = oops.language.getLangByID("loading_load_over");
        // 初始化帐号信息
        smc.account.login();

    }


    reset(): void {

    }
}